﻿/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is tyzoh.jp code.
 *
 * The Initial Developer of the Original Code is Kazutaka Kurihara.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** */
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using Qurihara;

namespace Qurihara
{
	public class LinkImageElement : ExternalElement
	{
        protected static ImageDLFactory imageDLFactory = new ImageDLFactory();
        public static ImageDLFactory ImageDLFactory
        {
            get { return imageDLFactory; }
            set { imageDLFactory = value; }
        }
        protected string hash;
        public string Hash
        {
            get { return hash; }
        }
        protected int refleshRate;
        public int RefleshRate
        {
            get { return refleshRate; }
        }
        protected bool persist;
        public bool Persist
        {
            get { return persist; }
        }

        protected bool downloading = false;

        protected ImageDLProxy idl;
//        public LinkImageElement(ImageDLProxy idlp,Control parentCont,int prio,Rectangle r, string name,string hash,int reflesh,bool persist) :base(parentCont,prio,r,name)
        public LinkImageElement(Control parentCont,int prio,Rectangle r, string name,string hash,int reflesh,bool persist) :base(parentCont,prio,r,name)
        {
            this.hash = hash;
            this.refleshRate = reflesh;
            this.persist = persist;

            //idl = idlp;
            idl = imageDLFactory.GetImageDLProxy();

            //for debug
            //filename = "http://localhost/test.jpg";

            idl.ImageDownloaded += new ImageDLProxy.SomethingDownloadedEventHandler(idl_ImageDownloaded);
            Download();

            if (refleshRate > 0)
            {
                //明示的にdisposeしないので、取り扱い注意！

                Timer t = new Timer();
                t.Tick += new EventHandler(t_Tick);
                t.Interval = refleshRate;
                t.Enabled = true;
                t.Start();
            }
        }

        protected void Download()
        {
            downloading = true;
            idl.Download(filename);
        }
        void t_Tick(object sender, EventArgs e)
        {
            Download();
        }

        void idl_ImageDownloaded(object o, ImageDLProxy.SomethingDownloadedEventArg e)
        {
            downloading = false;
            if (e.Something != null)
            {
                //ダウンロードが成功した
                string newhash = Qurihara.QBasic.DrawingUtil.GetHash((Bitmap)e.Something);
                if (hash != "")
                {
                    //もともとハッシュが設定されていた
                    if (hash != newhash)
                    {
                        //画像が更新された
                        if (persist)
                        {
                            //オリジナルじゃなきゃいやであるとき
                            idl.SetDefaultBitmap();
                        }
                        else
                        {
                            //どんどん更新したいとき
                            hash = newhash;
                        }
                    }
                    else
                    {
                        //ハッシュが同じなので、同じ画像である。
                    }
                }
                else
                {
                    //初めてハッシュを設定
                    hash = newhash;
                }

                //boundingBox.Width = idl.Bitmap.Width;
                //boundingBox.Height = idl.Bitmap.Height;
                Resize(0, 0, idl.Bitmap.Width - boundingBox.Width, idl.Bitmap.Height - boundingBox.Height);

                Delegate d = (MethodInvoker)delegate(){
                    //Invalidate();
                    parentControl.Invalidate();
                };
                parentControl.Invoke(d);

            }
        }

        public override void draw(Graphics g)
        {
            if (visible)
            {
                g.DrawImage(idl.Bitmap, BoundingBox);

                if (downloading)
                {
                    g.DrawString("Downloading...", tFont, tbrush, boundingBox.X, boundingBox.Bottom-32);
                }
                highlightDraw(g);
            }

        }
        protected override void highlightDraw(Graphics g)
        {
            base.highlightDraw(g);
            if (this.highlight)
            {
                g.DrawString(filename, tFont
                , tbrush, boundingBox.X, boundingBox.Y);
            }

        }
        public Bitmap Bitmap
        {
            get { return idl.Bitmap; }
        }
        //public Bitmap BitMap
        //{
        //    get
        //    {
        //        return image;
        //    }
        //    set
        //    {
        //        image = value;
        //        if (Qurihara.QBasic.Util.IsPropertyTrue("Base.HasSerializedBitmap"))
        //        {
        //            imageString = Qurihara.QBasic.DrawingUtil.BitmapBase64Serialize(image);
        //        }
        //    }
        //}
        //public string ImageString
        //{
        //    get { return imageString; }
        //}

        public void Reflesh()
        {
            //強制リフレッシュ
            Download();
        }
	}
}
